package 串联所有单词的子串;

import java.util.*;
import java.util.stream.Collectors;

public class Solution {

    public static void main(String[] args) {



        String input = "barfoothefoobarman";



        String[] params = new String[]{"foo","bar"};


        List<Integer> result = new ArrayList<>();
        int[] indexs = new int[params.length];
        List<String> tempBuffer = new ArrayList();
        Set<String> set = new HashSet();
        test(set,params,indexs,tempBuffer);


        for (int i = 0; i <= input.length() - 6; i++){
            String s = input.substring(i,6+i);
            System.out.println(s);
            if(set.contains(s)){
                result.add(i);
            }
        }

        result.stream().forEach(System.out::println);

    }

    public static void test(Set<String> set, String[] collect, int[] indexs, List<String> temp){

        for (int i = 0; i < collect.length; i++) {
            if(indexs[i] == 1){
                if(temp.size() == collect.length){
                    StringBuffer param = new StringBuffer();
                    temp.stream().forEach(item -> param.append(item));

                    set.add(param.toString());


                    return;
                }
                continue;
            }
            indexs[i] = 1;
            temp.add(collect[i]);
            test(set,collect,indexs,temp);
            indexs[i] = 0;

            temp.remove(temp.size()-1);
        }
    }


    public static void test2(List<Integer> result,String input,List<String> collect,int[] indexs,List<String> temp){

        for (int i = 0; i < collect.size(); i++) {
            if(indexs[i] == 1){
                if(temp.size() == collect.size()){
                    StringBuffer param = new StringBuffer();
                    temp.stream().forEach(item -> param.append(item));

                    if(input.indexOf(param.toString()) != -1){
                        System.out.println(param);
                        result.add(input.indexOf(param.toString()));
                    }

                    return;
                }
                continue;
            }
            indexs[i] = 1;
            temp.add(collect.get(i));
            test2(result,input,collect,indexs,temp);
            indexs[i] = 0;
            int currentIndex = i;
            while(i <= collect.size() - 2 && collect.get(currentIndex) == collect.get(i+1)){
                System.out.println(i);
                i++;
            }
            temp.remove(temp.size()-1);
        }
    }



}
